import { Component, OnInit, ViewChild } from '@angular/core';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';
import { ActivatedRoute } from '@angular/router';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { GlobalVariable } from '../../../../global.config';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';



//import {MyHttp} from '../../../../MyHttp';
declare var $: any;

@Component({
  selector: 'devinfo',
  templateUrl: './devinfo.component.html',
  styleUrls: ['./devinfo.component.css'],
  animations: [
    flyIn
  ]
})
export class Devinfo implements OnInit {
  @ViewChild('myModal') public myModal: ModalDirective;
  editdata: any;
  modify: boolean = false;
  modifyData = { qds: '', wbry: '' };
  lockerNumber: any;
  devdata: any;
  locker_longitude: any;
  locker_latitude: any;
  locker_category: any;
  module_number: string = '';
  imei_number: string = '';
  door_id: any;
  door_name: any;
  doordata: any;
  btype: number;
  operationType: string = '';
  private action: string = '';
  private param: any = { door_id: '', locker_device_id: '', door_use: '', door_switch: '' };

  constructor(private activeRoute: ActivatedRoute, private myHttp: MyHttp, private http: Http) { }
  ngOnInit() {
    this.activeRoute.params.subscribe(
      params => {
        this.lockerNumber = params['lockerNumber'];
        this.getdata();
      }

    );
  }

  getdata() {
    this.myHttp.post('locker/findById', { "locker_number": this.lockerNumber }).subscribe(res => {
      if (res.errorCode == 0) {
        this.devdata = res.message;
        if (!this.devdata.locker_map) {
          this.devdata.locker_map = ',';
        }
        this.devdata.longitude = this.devdata.locker_map.split(',')[0];
        this.devdata.latitude = this.devdata.locker_map.split(',')[1];
        this.getalldoor();
      }

    })
  }

  edit(data) {
    this.modify = true;
    this.locker_longitude = data.longitude;
    this.locker_latitude = data.latitude;
    this.locker_category = data.locker_category;
    this.imei_number = data.imei_number;
    this.module_number = data.module_number;
  }

  editsave() {
    this.devdata.locker_map = this.locker_longitude + ',' + this.locker_latitude;
    this.myHttp.post('locker/update', {
      "locker_number": this.lockerNumber,
      "locker_map": this.devdata.locker_map,
      "imei_number": this.imei_number,
      "module_number": this.module_number
    }).subscribe(res => {
      alert(res.message)
      if (res.errorCode == 0) {
        this.modify = false;
        this.getdata();
      }
    })
  }

  editdoor(obj) {
    this.btype = 1;
    this.myModal.show();
    this.door_name = obj.door_name;
    this.door_id = obj.id;
  }

  editdoorsave() {
    this.myHttp.post('door/update',
      {
        "door_name": this.door_name,
        "id": this.door_id
      }).subscribe(res => {
        if (res.errorCode == 0) {
          alert(res.message);
          this.cancel();
          this.getalldoor();
        }
      })
  }

  operation(str, type, door_name = '', id = "") {
    this.door_name = door_name;
    this.operationType = str;
    if (id) this.param.door_id = id;
    this.param.locker_device_id = this.devdata.locker_device_id;
    this.btype = 2;
    switch (type) {
      case 1://开门
        this.action = 'operateOne';
        this.param.door_use = 4;
        break;
      case 2://锁定
        this.action = 'operateOne';
        this.param.door_use = 0;
        break;
      case 3://解锁
        this.action = 'operateOne';
        this.param.door_use = 3;
        break;
      case 4:
        this.action = 'operateAll';
        this.param.door_switch = 1;
        break;
      case 5:
        this.action = 'operateAll';
        this.param.door_switch = 0;
        break;
      default:
        break;
    }
    this.myModal.show();
  }

  confirmOperation() {
    this.myHttp.post('door/' + this.action, this.param).subscribe(res => {
      if (res.errorCode == 0) {
        this.cancel();
        this.getalldoor();
        alert(this.operationType+'成功');
      }
      else {
        alert(res.message)
      }
    });
    // alert('需要跟硬件对接,暂未完成');
    this.cancel();
  }

  getalldoor() {
    if (this.devdata) {
      this.myHttp.post('door/findByLocker',
        {
          "locker_id": this.devdata.id

        }).subscribe(res => {
          if (res.errorCode == 0) {
            this.doordata = res.message;
          }
        })
    }

  }

  cancel() {
    this.myModal.hide();
  }


}

